#!/usr/bin/python

import os
import argparse
from lisn_utils import *

request = LISNRequest()
params = request.params

def get_files(vars, date, FTP, daily=False):
    '''
    Get magnetometer files from servers and update status
    '''
    
    #create folders
    if not os.path.exists(vars.output):
        os.makedirs(vars.output)
    if not os.path.exists(vars.temp):
        os.makedirs(vars.temp)
        
    sites = request.stations(instrument='magnetometer')
    if vars.sites:
        sites  = [s for s in sites if s['code'] in vars.sites.split(',')] 

    if not FTP.error:
        sitesbyserver = [s for s in sites if s['server']==vars.server]                  
        for site in sitesbyserver:
            dt1 = date-timedelta(1)
            dt2 = date          
            filename1 = '%s%sm' % (site['lisn_code'], dt1.strftime('%d%b.%y'))
            filename2 = '%s%sm' % (site['lisn_code'], dt2.strftime('%d%b.%y'))
            if vars.server=='montecillo':
                remote_file1 = os.path.join(vars.root, site['code'], 
                                            dt1.strftime('%Y'), filename1.lower())
                remote_file2 = os.path.join(vars.root, site['code'], 
                                            dt2.strftime('%Y'), filename2.lower())
            else:
                remote_file1 = os.path.join(vars.root, 
                                           '%s%s' % (site['lisn_code'], dt1.strftime('%y')), 
                                           filename1.lower())
                remote_file2 = os.path.join(vars.root, 
                                           '%s%s' % (site['lisn_code'], dt2.strftime('%y')), 
                                           filename2.lower())
            if daily:
                name = '%s_%s.min' % (site['code'], dt2.strftime('%y%m%d'))
                path = os.path.join(vars.output, site['network'], site['code'], 
                                        dt2.strftime('%Y/%m'), 'minute')
                if os.path.exists(os.path.join(path, name+'.gz')):
                    mag = MAGData(os.path.join(path, name+'.gz'))
                    mag.merge(MAGData(FTP.open(remote_file2)))
                else:
                    mag = MAGData(FTP.open(remote_file2))
                
                if len(mag)==0:
                    continue
                
                if not os.path.exists(path):
                    os.makedirs(path)
                mag.save(os.path.join(path, name), date=[dt2], gz=True)
                args2 = {'date': dt2.strftime('%y%m%d')}
                req = request.update_date(site['code'], 'magnetometer', 'minute', **args2)
            else:
                args1 = {}
                old_time = GPSDateTime(site['last_file_date']).timestamp
                try:                    
                    new_time = FTP.utime(remote_file2)
                except:
                    new_time = None   
                if new_time is not None and int(new_time)>int(old_time):
                    mag = MAGData(vars.temp+'%s.min' % site['code'])
                    if FTP.exists(remote_file1):
                        mag.merge(MAGData(FTP.open(remote_file1)))
                    if FTP.exists(remote_file2):
                        mag.merge(MAGData(FTP.open(remote_file2)))
                    if dt2.hour==0 and dt2.minute<7:
                        name = '%s_%s.min' % (site['code'], dt1.strftime('%y%m%d'))
                        path = os.path.join(vars.output, site['network'], site['code'], 
                                        dt1.strftime('%Y/%m'), 'minute')
                        mag.save(os.path.join(path, name), date=[dt1], gz=True)
                        args2 = {'date': dt1.strftime('%y%m%d')}
                        req = request.update_date(site['code'], 'magnetometer', 'minute', **args2)
                    mag.save(vars.temp+'%s.min' % site['code'], date=[dt1, dt2], gz=False)
                    new_file_date = GPSDateTime(int(new_time))
                    args1['last_file_date'] = new_file_date.strftime('%y%m%d_%H%M%S')
                    args1['last_file'] = filename2.lower()
                    args1['state'] = 1                    
                else:
                    args1['state'] = 2
                    print 'No new file for %s' % remote_file2
                req = request.update_station(site['code'], 'magnetometer', **args1)
                if req['error']:
                    print 'Error updating station:%s status' % site['code']                               
    
def main():
    '''
    Parse arguments to program.
    '''

    parser = argparse.ArgumentParser(description=main.func_doc)   
    # add options and arguments
    parser.add_argument('dates', nargs='*', default=[],
        help='Period of time to be processed (start and end date) fmt:yy,mm,dd')
    parser.add_argument('-s', '--sites', action='store',   
            help='stations four-char codes')
    parser.add_argument('-o', '--output', action='store', default='/data1/magnetometer/',
            help='Output path for save files')
    parser.add_argument('-t', '--temp', action='store', default='/data/temp/mag/',
            help='Path for temporary files')
    
    args = parser.parse_args()
    daily = False
    
    if len(args.dates)>0:
        daily = True
        try:
            date_1 = GPSDateTime(args.dates[0])
            if len(args.dates) == 2:
                date_2 = GPSDateTime(args.dates[1])
            else:
                date_2 = date_1
        except:
            parser.error('Dates can not be parsed fmt: yyyy,mm,dd')
    else:
        date_1 = GPSDateTime()
        date_2 = date_1
    
    for server in params.mag_server():
        args.server = server
        [host, username, password, args.root] = params.mag_server(server)
        FTP = MyFTP(host, username, password)
        date = date_1 
        while date<=date_2:
            print '*** Date: %s ***' % date.strftime('%Y/%m/%d')
            get_files(args, date, FTP, daily=daily)
            date += timedelta(1)
        FTP.close()
    
    #plot all mags
    if not daily:    
        fig = MyFigure(9, 1, figsize=(6, 10), figname='/data/temp/mag/magnetogram.png')
        sites = request.stations(instrument='magnetometer')
        for site in sites:        
            filename = '%s.min' % site['code']
            path = os.path.join('/data/temp/mag/', filename)
            data = MAGData(path, date=date_1.date())
            ax = plotter.plot_mag(data, station=site, figure=fig, scale=200)
            ax.ax.set_xlabel('')
        ax.ax.set_xlabel('Hour [UTC]')
        fig.show()
    
#Execute main routine
if __name__ == "__main__":
    print 'Start at ' + GPSDateTime().ctime()
    main()
    print 'Finish at ' + GPSDateTime().ctime()
    #FTP_inpe.close()
